<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Streams</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../using_io.html" title="Networking">
<link rel="prev" href="asio_refresher.html" title="Refresher">
<link rel="next" href="timeouts.html" title="Timeouts 💡">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="asio_refresher.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_io.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeouts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.using_io.stream_types"></a><a class="link" href="stream_types.html" title="Streams">Streams</a>
</h3></div></div></div>
<p>
        A <a class="link" href="../concepts/streams.html" title="Streams"><span class="emphasis"><em>Stream</em></span></a>
        is a communication channel where data is reliably transferred as an ordered
        sequence of bytes. Streams are either synchronous or asynchronous, and may
        allow reading, writing, or both. Note that a particular type may model more
        than one concept. For example, the networking types <a href="../../../../../../doc/html/boost_asio/reference/ip__tcp/socket.html" target="_top"><code class="computeroutput"><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span></code></a>
        and <a href="../../../../../../doc/html/boost_asio/reference/ssl__stream.html" target="_top"><code class="computeroutput"><span class="identifier">net</span><span class="special">::</span><span class="identifier">ssl</span><span class="special">::</span><span class="identifier">stream</span></code></a>
        support both <a class="link" href="../concepts/streams.html#beast.concepts.streams.SyncStream"><span class="emphasis"><em>SyncStream</em></span></a>
        and <a class="link" href="../concepts/streams.html#beast.concepts.streams.AsyncStream"><span class="emphasis"><em>AsyncStream</em></span></a>.
        All stream algorithms in Beast are declared as template functions using these
        concepts:
      </p>
<div class="table">
<a name="beast.using_io.stream_types.stream_concepts"></a><p class="title"><b>Table 1.4. Stream Concepts</b></p>
<div class="table-contents"><table class="table" summary="Stream Concepts">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Concept
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <a href="../../../../../../doc/html/boost_asio/reference/SyncReadStream.html" target="_top"><span class="emphasis"><em>SyncReadStream</em></span></a>
                </p>
              </td>
<td>
                <p>
                  Supports buffer-oriented blocking reads.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>
                </p>
              </td>
<td>
                <p>
                  Supports buffer-oriented blocking writes.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../concepts/streams.html#beast.concepts.streams.SyncStream"><span class="emphasis"><em>SyncStream</em></span></a>
                </p>
              </td>
<td>
                <p>
                  A stream supporting buffer-oriented blocking reads and writes.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a href="../../../../../../doc/html/boost_asio/reference/AsyncReadStream.html" target="_top"><span class="emphasis"><em>AsyncReadStream</em></span></a>
                </p>
              </td>
<td>
                <p>
                  Supports buffer-oriented asynchronous reads.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>
                </p>
              </td>
<td>
                <p>
                  Supports buffer-oriented asynchronous writes.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../concepts/streams.html#beast.concepts.streams.AsyncStream"><span class="emphasis"><em>AsyncStream</em></span></a>
                </p>
              </td>
<td>
                <p>
                  A stream supporting buffer-oriented asynchronous reads and writes.
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
        These template metafunctions check whether a given type meets the requirements
        for the various stream concepts, and some additional useful utilities. The
        library uses these type checks internally and also provides them as public
        interfaces so users may use the same techniques to augment their own code.
        The use of these type checks helps provide more concise errors during compilation:
      </p>
<div class="table">
<a name="beast.using_io.stream_types.type_traits_and_metafunctions"></a><p class="title"><b>Table 1.5. Type Traits and Metafunctions</b></p>
<div class="table-contents"><table class="table" summary="Type Traits and Metafunctions">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Name
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__executor_type.html" title="executor_type"><code class="computeroutput"><span class="identifier">executor_type</span></code></a>
                </p>
              </td>
<td>
                <p>
                  An alias for the type of object returned by <code class="computeroutput"><span class="identifier">get_executor</span></code>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__has_get_executor.html" title="has_get_executor"><code class="computeroutput"><span class="identifier">has_get_executor</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Determine if the <code class="computeroutput"><span class="identifier">get_executor</span></code>
                  member function is present.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__is_async_read_stream.html" title="is_async_read_stream"><code class="computeroutput"><span class="identifier">is_async_read_stream</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/AsyncReadStream.html" target="_top"><span class="emphasis"><em>AsyncReadStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__is_async_stream.html" title="is_async_stream"><code class="computeroutput"><span class="identifier">is_async_stream</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Determine if a type meets the requirements of both <a href="../../../../../../doc/html/boost_asio/reference/AsyncReadStream.html" target="_top"><span class="emphasis"><em>AsyncReadStream</em></span></a>
                  and <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__is_async_write_stream.html" title="is_async_write_stream"><code class="computeroutput"><span class="identifier">is_async_write_stream</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__is_sync_read_stream.html" title="is_sync_read_stream"><code class="computeroutput"><span class="identifier">is_sync_read_stream</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/SyncReadStream.html" target="_top"><span class="emphasis"><em>SyncReadStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__is_sync_stream.html" title="is_sync_stream"><code class="computeroutput"><span class="identifier">is_sync_stream</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Determine if a type meets the requirements of both <a href="../../../../../../doc/html/boost_asio/reference/SyncReadStream.html" target="_top"><span class="emphasis"><em>SyncReadStream</em></span></a>
                  and <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="../ref/boost__beast__is_sync_write_stream.html" title="is_sync_write_stream"><code class="computeroutput"><span class="identifier">is_sync_write_stream</span></code></a>
                </p>
              </td>
<td>
                <p>
                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>.
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
        Using the type checks with <code class="computeroutput"><span class="keyword">static_assert</span></code>
        on function or class template types will provide users with helpful error
        messages and prevent undefined behaviors. This example shows how a template
        function which writes to a synchronous stream may check its argument:
      </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">SyncWriteStream</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">write_string</span><span class="special">(</span><span class="identifier">SyncWriteStream</span><span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">,</span> <span class="identifier">string_view</span> <span class="identifier">s</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_write_stream</span><span class="special">&lt;</span><span class="identifier">SyncWriteStream</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
        <span class="string">"SyncWriteStream type requirements not met"</span><span class="special">);</span>
    <span class="identifier">net</span><span class="special">::</span><span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">net</span><span class="special">::</span><span class="identifier">const_buffer</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">size</span><span class="special">()));</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="asio_refresher.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_io.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeouts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
